package com.atlbb.controller;

import com.atlbb.mybatis.entity.Goods;
import com.atlbb.service.GoodsService;
import com.atlbb.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiangQingBin
 * @version 1.00
 * @ClassName GoodsController
 * @Description TODO
 * @time 2021/12/5 15:40
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    
    //查找所有商品
    @GetMapping("/findAll")
    public List<Goods> findAllGoods() {
        return goodsService.findAllGoods();
    }
    
    
    
    //保存一个商品
    @PostMapping("/saveOne")
    public Result saveOneGoods(@RequestBody Goods goods) {
        Result result = new Result();
        try {
            goodsService.saveOneGoods(goods);
            result.setStatue(true);
            result.setMsg("添加成功!");
        } catch (Exception e) {
            result.setStatue(false);
            result.setMsg("添加失败!");
        }
        return result;
    }
    
    
    
    //删除一个商品
    @GetMapping("/deleteOneById")
    public Result deleteOneGoodsById(@RequestParam("g_id") Long g_id){
        Result result = new Result();
        try {
            goodsService.deleteOneGoodsById(g_id);
            result.setStatue(true);
            result.setMsg("删除成功!");
        } catch (Exception e){
            result.setStatue(false);
            result.setMsg("删除失败!");
        }
        return result;
    }
    
    //更新一个商品
    @PostMapping("/updateOneGoods")
    public Result updateOneGoods(@RequestBody Goods goods){
        Result result = new Result();
        try {
            goodsService.updateOneGoods(goods);
            result.setStatue(true);
            result.setMsg("编辑成功!");
        } catch (Exception e){
            result.setStatue(false);
            result.setMsg("编辑失败!");
        }
        return result;
    }
    //购买商品
    @GetMapping("/buyGoods")
   public Result buyGoods(@RequestParam("gid") Long id,@RequestParam("num") Long num){
       Result result = new Result();
       try {
           goodsService.buyGoods(id,num);
           result.setStatue(true);
           result.setMsg("购买成功!");
       }catch (Exception e){
           result.setStatue(false);
           result.setMsg("购买失败!");
       }
       return result;
   }
}
